package scau.redi.social_platform.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scau.redi.social_platform.common.util.ResponsePageUtil;
import scau.redi.social_platform.dao.*;
import scau.redi.social_platform.domain.pojo.Shared;
import scau.redi.social_platform.domain.pojo.SharedCollection;
import scau.redi.social_platform.domain.pojo.SharedLiked;
import scau.redi.social_platform.domain.vo.BriefSharedVO;
import scau.redi.social_platform.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author redi
 * @since 2022-05-05
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    SharedMapper sharedMapper;
    @Autowired
    SharedContentMapper sharedContentMapper;
    @Autowired
    SharedImgMapper sharedImgMapper;
    @Autowired
    SharedCollectionMapper sharedCollectionMapper;
    @Autowired
    SharedLikedMapper sharedLikedMapper;
    @Autowired
    SharedCommentMapper sharedCommentMapper;

    @Override
    public int collectShared(SharedCollection sharedCollection) {
        return sharedCollectionMapper.insertSelective(sharedCollection);
    }

    @Override
    public int unCollectShared(Integer sharedId, Integer userId) {
        return sharedCollectionMapper.deleteBySharedIdAndUserId(sharedId, userId);
    }

    @Override
    public int likeShared(SharedLiked sharedLiked) {
        return sharedLikedMapper.insertSelective(sharedLiked);
    }

    @Override
    public int unLikeShared(Integer sharedId, Integer userId) {
        return sharedLikedMapper.deleteBySharedIdAndUserId(sharedId, userId);
    }

    @Override
    public ResponsePageUtil<BriefSharedVO> getAllCollectShared(Integer userId, int page, int size) {
        List<Shared> sharedList = new ArrayList<>();
        PageHelper.startPage(page, size);
        List<SharedCollection> sharedCollectionList = sharedCollectionMapper.getAllByUserId(userId);
        for (SharedCollection sharedCollection:sharedCollectionList){
            Shared shared = sharedMapper.selectByPrimaryKey(sharedCollection.getSharedId());
            sharedList.add(shared);
        }

        List<BriefSharedVO> VOList = new ArrayList<>();
        for (Shared shared : sharedList) {
            BriefSharedVO vo = new BriefSharedVO();
            vo.setSharedId(shared.getId());
            vo.setUserId(shared.getUserId());
            vo.setContent(sharedContentMapper.selectBySharedId(shared.getId()).getContent());
            vo.setImgList(sharedImgMapper.selectBySharedId(shared.getId()));
            vo.setCollectedSum(sharedCollectionMapper.selectBySharedId(shared.getId()).size());
            vo.setLikedSum(sharedLikedMapper.selectBySharedId(shared.getId()).size());
            vo.setCommentSum(sharedCommentMapper.selectBySharedId(shared.getId()).size());
            vo.setCreateTime(shared.getCreateTime());
            VOList.add(vo);
        }

        // 把分页的list传入（包含分页信息），后面再把data内容换为VOList
        ResponsePageUtil<SharedCollection> sharedWithContentResponsePageUtil = ResponsePageUtil.restPage(sharedCollectionList);

        ResponsePageUtil<BriefSharedVO> pageUtil = new ResponsePageUtil<BriefSharedVO>(
                page,
                size,
                sharedWithContentResponsePageUtil.getTotalPage(),
                (int) sharedWithContentResponsePageUtil.getTotalItemNum(),
                VOList);
        return pageUtil;
    }

    @Override
    public ResponsePageUtil<BriefSharedVO> getAllLikedShared(Integer userId, int page, int size) {
        List<Shared> sharedList = new ArrayList<>();
        PageHelper.startPage(page, size);
        List<SharedLiked> sharedLikedList = sharedLikedMapper.getAllByUserId(userId);
        for (SharedLiked sharedLiked:sharedLikedList){
            Shared shared = sharedMapper.selectByPrimaryKey(sharedLiked.getSharedId());
            sharedList.add(shared);
        }

        List<BriefSharedVO> VOList = new ArrayList<>();
        for (Shared shared : sharedList) {
            BriefSharedVO vo = new BriefSharedVO();
            vo.setSharedId(shared.getId());
            vo.setUserId(shared.getUserId());
            vo.setContent(sharedContentMapper.selectBySharedId(shared.getId()).getContent());
            vo.setImgList(sharedImgMapper.selectBySharedId(shared.getId()));
            vo.setCollectedSum(sharedCollectionMapper.selectBySharedId(shared.getId()).size());
            vo.setLikedSum(sharedLikedMapper.selectBySharedId(shared.getId()).size());
            vo.setCommentSum(sharedCommentMapper.selectBySharedId(shared.getId()).size());
            vo.setCreateTime(shared.getCreateTime());
            VOList.add(vo);
        }

        // 把分页的list传入（包含分页信息），后面再把data内容换为VOList
        ResponsePageUtil<SharedLiked> sharedWithContentResponsePageUtil = ResponsePageUtil.restPage(sharedLikedList);

        ResponsePageUtil<BriefSharedVO> pageUtil = new ResponsePageUtil<BriefSharedVO>(
                page,
                size,
                sharedWithContentResponsePageUtil.getTotalPage(),
                (int) sharedWithContentResponsePageUtil.getTotalItemNum(),
                VOList);
        return pageUtil;
    }

    @Override
    public ResponsePageUtil<BriefSharedVO> getAllMyShared(Integer userId, int page, int size) {
        List<Shared> sharedList = sharedMapper.selectByUserId(userId);
        PageHelper.startPage(page, size);

        List<BriefSharedVO> VOList = new ArrayList<>();
        for (Shared shared : sharedList) {
            BriefSharedVO vo = new BriefSharedVO();
            vo.setSharedId(shared.getId());
            vo.setUserId(shared.getUserId());
            vo.setContent(sharedContentMapper.selectBySharedId(shared.getId()).getContent());
            vo.setImgList(sharedImgMapper.selectBySharedId(shared.getId()));
            vo.setCollectedSum(sharedCollectionMapper.selectBySharedId(shared.getId()).size());
            vo.setLikedSum(sharedLikedMapper.selectBySharedId(shared.getId()).size());
            vo.setCommentSum(sharedCommentMapper.selectBySharedId(shared.getId()).size());
            vo.setCreateTime(shared.getCreateTime());
            VOList.add(vo);
        }

        // 把分页的list传入（包含分页信息），后面再把data内容换为VOList
        ResponsePageUtil<Shared> sharedWithContentResponsePageUtil = ResponsePageUtil.restPage(sharedList);

        ResponsePageUtil<BriefSharedVO> pageUtil = new ResponsePageUtil<BriefSharedVO>(
                page,
                size,
                sharedWithContentResponsePageUtil.getTotalPage(),
                (int) sharedWithContentResponsePageUtil.getTotalItemNum(),
                VOList);
        return pageUtil;
    }
}
